package annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @Author: Hevery
 * @Description:
 * @Date: Created on 2017/9/12
 * @Modified By:
 */
public class ParseAnnotation {
    public static void main(String[] args) {
        try {
            //1.使用类加载器加载类
            Class c = Class.forName("annotation.Apple");
            //2.找到类上面的注解
            boolean isExist = c.isAnnotationPresent(Description.class);
            if(isExist){
                Description des = (Description) c.getAnnotation(Description.class);
                System.out.println(des.value());
            }
            //3.找到方法上的注解
            Method[] methods = c.getMethods();
            for(Method m:methods){
                boolean isMExist = m.isAnnotationPresent(Description.class);
                if(isMExist){
                    Description desm =  (Description)m.getAnnotation(Description.class);
                    System.out.println("first method--->"+desm.value());
                }
            }
            //4.另一种方式解析注解
            for(Method m:methods){
                Annotation[] as = m.getAnnotations();
                for(Annotation a: as){
                    if(a instanceof Description){
                        Description d = (Description) a;
                        System.out.println("second method--->"+d.value());
                    }
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
